package com.gimi.cloud.bbp.merchantZone.consumer.controller;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameEnum;
import com.gimi.cloud.bbp.common.utils.CommonUtil;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.merchantZone.consumer.service.product.ProductService;
import com.gimi.cloud.bbp.merchantZone.consumer.service.product.SkuService;
import com.gimi.cloud.bbp.merchantZone.consumer.service.store.StoreService;
import com.gimi.cloud.bbp.merchantZone.consumer.service.ueditor.UeditorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by luck on 2017/09/28.
 */
@RestController
public class MerChantMicroServiceConsumerController {

	private static Logger logger = LoggerFactory.getLogger(MerChantMicroServiceConsumerController.class);
	private final ProductService productService;
	private final StoreService storeService;
	private final SkuService skuService;
	private final UeditorService ueditorService;
	@Autowired
	public MerChantMicroServiceConsumerController(ProductService productService,
												  StoreService storeService,SkuService skuService, UeditorService ueditorService) {
		this.productService = productService;
		this.storeService = storeService;
		this.skuService = skuService;
		this.ueditorService = ueditorService;
	}

	/**
	 * 作为user服务的统一入口.
	 *
	 * @param requestData
	 *            请求参数
	 * @param request
	 *            httpRequest
	 * @param response
	 *            response
	 */
	@RequestMapping(value = "service")
	public void merchantZoneService(@RequestBody(required = false) String requestData, @RequestParam(value = "file",required = false) MultipartFile file,
									HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("===>进入商城微服务主程序入口.");
		logger.info("token===>" + request.getHeader("authorization"));
		logger.info("serviceName===>" + request.getHeader("serviceName"));

		/**
		 * 讲网关中的添加人更新人属性放入body
		 */
		Map<String,String> headerMaps = new HashMap<>();
		//设置请求body的key为authToken，对应头的key为authorization
		headerMaps.put("authToken","authorization");
		String[] headersKeys = new String[]{"ip","authToken","uid","uname","serviceName"};
		requestData = CommonUtil.initHeaderInfoToBody(request,requestData,headersKeys,headerMaps,false);
		Response resp = new Response();

		ServiceNameEnum serviceNameEnum = ServiceNameEnum.getStatus(request.getHeader("serviceName"));
		switch (serviceNameEnum) {
			case PRODUCT_LIST:
				productService.productList(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case ADD_PRODUCT:
				productService.addProduct(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case DELETE_PRODUCT:
				productService.deleteProduct(requestData, response, request);
				break;
			case UP_SHELF_PRODUCT:
				productService.upShelfProduct(requestData, response, request);
				break;
			case OFF_SHELF_PRODUCT:
				productService.offShelfProduct(requestData, response, request);
				break;
			case GET_PRODUCT:
				productService.getProduct(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case UPDATE_PRODUCT:
				productService.updateProduct(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case GET_STORE_CATS:
				productService.getStoreCats(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case RELEASE_PRODUCT:
				productService.releaseProduct(requestData, response, request);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
			case MERCHANT_FILE_UPLOAD://商品文件上传
				productService.fileUpload(requestData,file,response,request);
				break;
			case STORE_ADD://商铺添加
				storeService.addStore(requestData, request, response);
				break;
			case STORE_UPDATE://商铺修改
				storeService.updateStore(requestData, request, response);
				break;
			case STORE_LIST://商铺列表
				storeService.storeList(requestData, request, response);
				break;
			case STORE_ALL_LIST://某商户所有店铺列表
				storeService.allStoreList(requestData, request, response);
				break;
			case STORE_SINGLE://获取单个店铺
				storeService.storeSingle(requestData, request, response);
				break;
			case STORE_AUTH://店铺审核
				storeService.storeAuth(requestData, request, response);
				break;
			case STORE_DELETE://商铺删除
				storeService.deleteStore(requestData, request, response);
				break;
			case STORE_UPLOAD://商品文件上传
				storeService.fileUpload(response,request);
				break;
			case MERCHANT_STORE_ADD://商铺经营范围添加
				storeService.addMerchantStore(requestData, request, response);
				break;
			case MERCHANT_STORE_ADD_LIST://商铺可添加经营范围列表
				storeService.merchantStoreAddList(requestData, request, response);
				break;
			case MERCHANT_STORE_LIST://商铺经营范围列表
				storeService.merchantStoreList(requestData, request, response);
				break;
			case MERCHANT_STORE_DELETE://商铺经营范围删除
				storeService.deleteMerchantStore(requestData, request, response);
				break;
				
			case SKU_LIST://根据product_id获取Sku列表
				skuService.listSku(requestData, request, response);
				break;
			case SKU_MODEL_DETAIL://sku模板属性关系转化
				skuService.SkuModelDetail(requestData, request, response);
				break;
			case SKU_UPDATE://保存Sku列表
				skuService.updateSku(requestData, request, response);
				break;
			case SKU_DELETE://删除一条Sku数据
				skuService.deleteSku(requestData, request, response);
				break;
			case SKU_IMG://查看Sku图片
				skuService.skuImg(requestData, request, response);
				break;
			case UEDITOR_ACTION://Ueditor插件操作接口
				ueditorService.ueditorAction(request, response);
			default:
				resp.setResponseCode(ResponseCodeEnum.SERVICE_NAME_NOT_FOUND);
				Util.sendResponse(JsonUtil.toJson(resp), response);
				break;
		}
		logger.info("===>执行完毕.\n");
	}


}
